#include "widget.h"
#include "ui_widget.h"

#include <QtMath>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    //this->setStyleSheet("background-color: black");
    //固定窗口大小
    setFixedSize(800,600);

    //起始角度
    StartingAngle = 150;
    currentValue = 0;
    mark = 0;

    startSpeed();
}

Widget::~Widget()
{
    delete ui;
}

void Widget::startSpeed()
{
    timer = new QTimer(this);
    connect(timer,&QTimer::timeout,[=](){
        if(mark == 0)
        {
            currentValue++;
            if(currentValue >=61)
                mark = 1;
        }
        if(mark == 1)
        {
            currentValue--;
            if(currentValue <=0)
                mark = 0;
        }
        update();
    });
    timer->start(50);
}

void Widget::drawBackground(QPainter &painter)
{
    QRadialGradient radialGradient(rect().center(),300);
    radialGradient.setColorAt(0,QColor(0,0,0,210));
    radialGradient.setColorAt(1,QColor(0,0,0,235));
    painter.setBrush(radialGradient);
    painter.drawRect(rect());
}

void Widget::initCanvas(QPainter &painter)
{
    //坐标系平移到窗口中心
    QPoint cent(rect().width()/2,rect().height()*0.6);
    painter.translate(cent);
}

void Widget::drawMiddleCircle(QPainter &painter,int radius)
{
    //画小圆
    //painter.setPen(QPen(Qt::white,3));
    painter.drawEllipse(QPoint(0,0),radius,radius);
}

void Widget::drawCurrentSpeed(QPainter &painter)
{
    //设置字体
    //QFont::QFont(const QString &family, int pointSize = -1, int weight = -1, bool italic = false)
    painter.setPen(Qt::white);
    QFont font("Arial",30);
    font.setBold(true);
    painter.setFont(font);

    //画当前速度值
    //Qt::AlignCenter对其中心
    painter.drawText(QRect(-60,-60,120,70),Qt::AlignCenter,QString::number(currentValue*4));
    QFont font2("Arial",15);
    font.setBold(true);
    painter.setFont(font2);
    painter.drawText(QRect(-60,-60,120,160),Qt::AlignCenter,"km/h");
}

void Widget::drawScale(QPainter &painter,int radius)
{
    //画刻度
    //1.算出一个刻度需要旋转的角度
    angle = 240*1.0/60;//240*1.0是为了扩大变脸类型，把int转换为double，这样才能保留小数

    //保留当前坐标位置，此时在原点，x在3点钟方向
    painter.save();

    //2.设置第一个刻度的位置
    painter.rotate(StartingAngle);
    painter.setPen(QPen(Qt::white,4));
    for(int i = 0;i<=60;i++)
    {
        if(angle*i>=160)
        {
            painter.setPen(QPen(Qt::red,4));
        }
        else
        {
            painter.setPen(QPen(Qt::white,4));
        }
        if(i%5 == 0)
        {
            //画字第一种方式，画刻度的同时画字，但这种方式字只能以坐标轴方向排列
            //            if(135 + angle *i <240)
            //            {
            //                painter.rotate(180);
            //                painter.drawText(-height()/2+25,7,QString::number(i*4));
            //                painter.rotate(-180);
            //            }
            //            else
            //            {
            //                painter.drawText(height()/2-57,7,QString::number(i*4));
            //            }

            //画长刻度
            painter.drawLine(radius-20,0,radius -3,0);
        }
        else
        {
            //画短刻度
            painter.drawLine(radius-7,0,radius -3,0);
        }
        //旋转
        painter.rotate(angle);
    }
    painter.setPen(QPen(Qt::white,4));
    painter.restore();
}

void Widget::drawScaleText(QPainter &painter,int radius)
{
    int delR = radius-49;
    QFont font("Arial",17);
    font.setBold(true);
    painter.setFont(font);
    painter.setPen(QPen(Qt::white,4));
    //画字第二种方式,移动坐标轴的同时画字
    for(int i = 0;i<=60;i++)
    {
        if(i%5 == 0)
        {
            //保存坐标系
            painter.save();

            //算出平移点  sinx=y/r,cosx=x/r; y=sinx*r,x=cosx*r
            //qCos和qSin中的度数用弧度表示,即用qDegreesToRadians()将度数转换为弧度
            //弧度=角度*3.14/180
            int delX = qCos((210 - angle*i)*M_PI/180) * delR;
            int delY = qSin(qDegreesToRadians(210 - angle*i)) * delR;

            //平移坐标系,在Qt的默认坐标中，y轴向下为正方向，所以y取负让坐标轴向上
            painter.translate(QPoint(delX,-delY));
            //旋转坐标系
            painter.rotate(-120 + angle*i);//angle=240/60=4,i*4=120时，让实参为0，即可实现坐标轴-y朝向刻度

            //写上文字
            //在一个左上角坐标为(-20,-20)，长50宽30的矩形中心写上文字
            painter.drawText(-25,-25,50,30,Qt::AlignCenter,QString::number(i*4));
            //恢复坐标系
            painter.restore();
        }
    }
}

void Widget::drawPointer(QPainter &painter,int length)
{
    //画指针和线
    painter.save();
    //painter.drawLine(60,0,length,0);
    painter.setBrush(Qt::white);
    painter.setPen(Qt::NoPen);
    static const QPointF points[4] = {
        QPointF(0.0,0.0),
        QPointF(200.0, -1.1),
        QPointF(200.0, 1.1 ),
        QPointF(0.0, 15.0),
    };
    painter.rotate(StartingAngle + angle * currentValue);
    painter.drawPolygon(points, 4);
    painter.restore();
}

void Widget::drawSpeedPie(QPainter &painter,int radius)
{
    //画扇形
    QRect rectangle(-radius,-radius,radius*2,radius*2);
    //painter.drawRect(rectangle);
    painter.setPen(Qt::NoPen);
    painter.setBrush(QColor(255,0,0,50));
    painter.drawPie(rectangle,(360-StartingAngle)*16,-angle*currentValue*16);//angle前负数为顺时针，正数为逆时针
}

void Widget::drawEllipseInnerBlack(QPainter &painter, int radius)
{
    painter.setBrush(QColor(0,0,0,255));
    painter.setPen(Qt::NoPen);
    painter.drawEllipse(QPoint(0,0),radius,radius);
}

void Widget::drawEllipseInnerShine(QPainter &painter, int radius)
{
    QRadialGradient radialGradient(0,0,radius);

    radialGradient.setColorAt(0.0,QColor(255,0,0,180));
    radialGradient.setColorAt(1.0,QColor(0,0,0,20));
    painter.setBrush(radialGradient);
    painter.drawEllipse(QPoint(0,0),radius,radius);
}

void Widget::drawEllipseOutterShine(QPainter &painter, int radius)
{
    //画扇形
    QRect rectangle(-radius,-radius,radius*2,radius*2);
    //painter.drawRect(rectangle);
    painter.setPen(Qt::NoPen);
    QRadialGradient radialGradient(0,0,radius);
    radialGradient.setColorAt(1,QColor(255,0,0,200));
    radialGradient.setColorAt(0.99,QColor(255,0,0,120));
    radialGradient.setColorAt(0.91,QColor(0,0,0,0));
    radialGradient.setColorAt(0,QColor(0,0,0,0));
    painter.setBrush(radialGradient);
    painter.drawPie(rectangle,(360-StartingAngle)*16,-angle*60*16);//angle前负数为顺时针，正数为逆时针
}

void Widget::drawLogo(QPainter &painter, int radius)
{
    painter.setBrush(QBrush(Qt::white));
    painter.drawEllipse(-40.0,radius/2,80,80);

    QRect rectangle(-40.0,radius/2,80,80);
    painter.drawPixmap(rectangle,QPixmap(":/Index/BMW.png"));
}

void Widget::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing,true);
    int rad = height()/2;

    //画背景
    drawBackground(painter);
    //坐标系平移到窗口中心
    initCanvas(painter);
    //画小圆
    drawMiddleCircle(painter,80);
    //画刻度
    drawScale(painter,rad);
    //画刻度文字
    drawScaleText(painter,rad);
    //画指针
    drawPointer(painter,rad-60);
    //画扇形
    drawSpeedPie(painter,rad+22);
    //画第二层渐变内圈
    drawEllipseInnerShine(painter,140);
    //画黑色内圈
    drawEllipseInnerBlack(painter,80);
    //画当前速度值
    drawCurrentSpeed(painter);
    //画外环渐变
    drawEllipseOutterShine(painter,rad+20);
    //画Logo
    drawLogo(painter,rad);
}



